package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.crypto.digest.BCrypt;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.LoginDto;
import com.zzyl.entity.User;
import com.zzyl.mapper.UserMapper;
import com.zzyl.service.UserService;
import com.zzyl.utils.JwtUtil;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.UserVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {
    private final UserMapper userMapper;
    @Override
    public UserVo login(LoginDto loginDto) {
        String password = loginDto.getPassword();
        password= BCrypt.hashpw(password);

        UserVo userVo =userMapper.login(loginDto.getUsername());
        if (!ObjectUtil.isNotEmpty(userVo)){
            throw new RuntimeException("用户名或密码错误");
        }
        userVo.setPassword("");

        Map<String,Object> map = new HashMap<>();
        map.put("username",loginDto.getUsername());

        userVo.setUserToken(JwtUtil.createJWT("itheima",600000,map));

        return userVo;
    }
}
